package com.mantou.cdl.proxy;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.httpclient.HttpException;
import org.apache.commons.io.IOUtils;
import org.dom4j.DocumentException;

import com.alibaba.fastjson.JSONObject;
import com.mantou.cdl.extractor.XmlModeFetcher;
import com.mantou.cdl.net.HttpInvoker;
import com.mantou.cdl.net.HttpResult;
import com.mantou.cdl.utils.BloomFilter;
import com.mantou.cdl.utils.BloomFilter32bit;
import com.mantou.cdl.utils.SysConfig;


/**
 * @author dwjmantou
 * support:www.mantoub.com
 * @date 2015年5月4日
 * @version 1.0
 * get proxy resource from outer of system,so this code maybe always abate
 */
public class XICIProxyImporter implements AbstractProxyImporter{
	private static int xici_pagenumber = 1;

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		List<ProxyAddress> proxys = getfrom_xici(200,new BloomFilter32bit(SysConfig.proxyspace, 20));
		System.out.println(proxys.size());
	}

	public static List<ProxyAddress> getfrom_xici(int expectedNumbers,BloomFilter allocatedSource){
		String baseUrl = "http://www.xici.net.co/nn/";
		List<ProxyAddress> ret = new ArrayList<ProxyAddress>();
		int getnumbers = 0;
		try {
			XmlModeFetcher fetcher = new XmlModeFetcher(IOUtils.toString(ProxyAddress.class.getResourceAsStream("/com/mantou/cdl/fetch_mode_xici.xml")));
			while(true){
				HttpResult res = new HttpInvoker(baseUrl+xici_pagenumber++, 0, null).request();
				if(res.getStatusCode() == 200){
					List<String> retjson = fetcher.fetch(new String(res.getResponseBody()));
					for(String jsonItem: retjson){
						//System.out.println(jsonItem);
						JSONObject jsonObject = JSONObject.parseObject(jsonItem);
						ProxyAddress proxyitem = new ProxyAddress();
						proxyitem.setIp(jsonObject.getString("ip"));
						
						if(allocatedSource.contains(proxyitem.getIp()))
							continue;
						proxyitem.setPort(Integer.parseInt(jsonObject.getString("port")));
						proxyitem.setProto(jsonObject.getString("proto"));
						proxyitem.setType(1);
						ret.add(proxyitem);
						getnumbers ++;
					}
				}
				if(getnumbers >= expectedNumbers){
					break;
				}
			}
		} catch (HttpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ret;
	}

	public List<ProxyAddress> getSource(int expecteNumber, BloomFilter allocatedSource) {
		// TODO Auto-generated method stub
		return getfrom_xici(expecteNumber,allocatedSource);
	}
}
